// pages/activity-list/activity-list.js
const app = getApp();
const request = require('../../utils/request.js');
const config = require('../../utils/config');
const util = require('../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    classifyId: "",
    colorTheme: app.globalData.colorTheme,
    isCard: false, // 列表每项样式类型
    pageNum: 1,
    pageSize: 10,
    params: {},
    hasResult: true,
    classifyList: [
      {
        cname: "全部",
        id: ""
      }
    ],
    classifyIndex: null,
    activityList: [],
    TabCur: 0,
    orderList: [
      {
        name: '默认排序',
        order: ''
      },
      {
        name: '最多预约',
        order: 'appointment_count'
      },
      {
        name: '最新发布',
        order: 'create_time'
      }
    ],
    isFixed: false
  },

  /**
   * 提交搜索内容
   */
  submitSearch(e){
    let query = e.detail.value;
    this.setData({
      query,
      pageNum: 1,
      params: { 
        title: query,
        classifyId: this.data.classifyId
      }
    });
    this.getActivityList(this.data.pageNum, this.data.pageSize, this.data.params);
  },
  
  /**
   * 监听搜索文本框内容变化
   */
  changeQuery(e){
    this.setData({
      activityList: [],
      params: {
        title: e.detail.value,
        order: this.data.params.order,
        classifyId: this.data.classifyId
      }
    });
    this.getActivityList(this.data.pageNum, this.data.pageSize, this.data.params);
  },

  /**
   * 清空搜索文本框
   */
  clearQuery(){
    this.setData({
      params: {},
      classifyIndex: null,
      classifyId: null
    });
  },


  /**
   * 切换标签
   */
  tabSelect(e) {
    let index = e.currentTarget.dataset.id;
    this.setData({
      TabCur: index,
      pageNum: 1,
      activityList: [],
      hasResult: true,
      params: {
        title: this.data.params.title,
        order: this.data.orderList[index].order,
        classifyId: this.data.classifyId
      }
    });
    this.getActivityList(this.data.pageNum, this.data.pageSize, this.data.params);
  },

  /**
   * 获取活动列表
   */
  getActivityList(pageNum, pageSize, params){
    let token = wx.getStorageSync("token");
    let url = config.SERVER_URL + 'activity/list/activities?pageNum=' + pageNum + '&pageSize=' + pageSize;
    wx.showLoading({
      title: '加载ing'
    });
    request.requestGetApi(url, token, params, 'application/json').then(res=>{
      if(res.status === config.STATUS_OK){
        res.data.rows.forEach(item => {
          item.actStatus = util.computedTimeStatus(item.startTime, item.endTime);
        });
        let hasResult = res.data.rows.length >= pageSize;
        this.setData({
          hasResult,
          activityList: this.data.activityList.concat(res.data.rows)
        });
      }
    }).catch(err=>{
      app.failFun(err);
    }).finally((res) => {
      app.completeFun(res);
    });
  },
  
  /**
   * 获取分类列表
   */
  getClassifyList(){
    let url = config.SERVER_URL + 'activity/list/activity/classify';
    wx.showLoading({
      title: '加载分类ing'
    });
    request.requestGetApi(url, null, null, 'application/json').then(res=>{
      this.setData({
        classifyList: this.data.classifyList.concat(res.data)
      });
    }).catch(err=>{
      app.failFun(err);
    }).finally((res) => {
      app.completeFun(res);
    });
  },

  /**
   * 分类选择变化
   */
  classifyPickerChange: function(e) {
    let that = this;
    let index = e.detail.value;
    let classifyId = this.data.classifyList[index].id;
    console.log(index);
    this.setData({
      activityList: [],
      classifyIndex: index,
      classifyId: classifyId,
      params: {
        title: this.data.params.title,
        order: this.data.params.order,
        classifyId: classifyId
      }
    });
    that.getActivityList(this.data.pageNum, this.data.pageSize, this.data.params);
  },

  /**
   * 跳转到活动详情页面
   */
  toActivityInfo(e){
    wx.navigateTo({
      url: '/pages/activity-info/activity-info?id=' + e.currentTarget.dataset.id
    });
  },

  /**
   * 实时监控滚动距离顶部的位置
   */
  onPageScroll:function(e){
    let isFixed;
    if(e.scrollTop >= 50){
      isFixed = true;
    }else{
      isFixed = false;
    }
    this.setData({
      isFixed
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let index = parseInt(options.orderid);
    let orderObj = this.data.orderList[index];
    if(options.orderid){    
      this.setData({
        params: {
          title: "",
          order: orderObj.order
        },
        TabCur: index
      });
    }
    this.getActivityList(this.data.pageNum, this.data.pageSize, this.data.params);
    this.getClassifyList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.isResult){
      return;
    }
    let pageNum = this.data.pageNum + 1;
    this.setData({
      pageNum: pageNum
    });
    this.getActivityList(pageNum, this.data.pageSize, this.data.params);
  },
})